﻿#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace VisualWebGuiApplication2
{
    public partial class SpecialOperation : UserControl
    {
        
        public SpecialOperation()
        {
            InitializeComponent();
            this.RightToLeft = Gizmox.WebGUI.Forms.RightToLeft.Yes;

           
        

          

        }
       
     

        private void btnOpenTank_Click(object sender, EventArgs e)
        {
          
           

            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                 Accompanying_stocks stocktodel = db.Accompanying_stocks.FirstOrDefault(x => (x.makat == txtMakat.Text));
                 if (null != stocktodel)
                {
                    db.Accompanying_stocks.DeleteObject(stocktodel);
                  
                    if (db.SaveChanges() > 0)
                    {
                        MessageBox.Show("נמחק בהצלחה");
                        return;
                    }
                    
                } 




               
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                if(string.IsNullOrEmpty(txtTankNumOld.Text))
                {
                
                MessageBox.Show("חובה להזין מס צובר ישן ");
                return;
                }
                if (string.IsNullOrEmpty(txtTankNumnew.Text))
                {

                    MessageBox.Show("חובה להזין מס צובר חדש ");
                    return;
                }
                Tank_Stocks tank = db.Tank_Stocks.SingleOrDefault(x=>x.TankNumber == txtTankNumOld.Text);
                if(null!=tank)
                {
                tank.TankNumber =txtTankNumnew.Text;
                    if(db.SaveChanges()>0)
                    {
                    MessageBox.Show("שינוי מס צובר בוצע בהצלחה ");
                        return;

                    }
                
                }else
                {
                MessageBox.Show("צובר לא קיים");
                    return;
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txttankTofix.Text))
            {

                MessageBox.Show("חובה להזין מס צובר  ");
                return;
            }

            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                Tank_Stocks tanktofix = db.Tank_Stocks.FirstOrDefault(x => x.TankNumber == txttankTofix.Text);
                if (null != tanktofix)
                { 
                //make status 2 
                    tanktofix.TankSystemStatus = "2";
                //move fix operations to history  
                    TankFixOperation optofix = db.TankFixOperations.FirstOrDefault(x => x.tankID == tanktofix.Id);
                    if (null != optofix)
                    {
                        // move to history then delete  
                        //        TankFixOperation



                        List<TankFixOperation> _opList = db.TankFixOperations.Where(x => x.tankID == tanktofix.Id).ToList();
                        //   List<TankFixOperations_history> _opList_history=new List<TankFixOperations_history>();
                        foreach (TankFixOperation item in _opList)
                        {
                            TankFixOperations_history row = new TankFixOperations_history();
                            row.tankID = item.tankID;
                            row.amount = item.amount;
                            row.date = item.date;
                            row.FixoperationID = item.FixoperationID;
                            row.note = item.note;
                            row.title = item.title;
                            //ITERATE THROUGHT listview and save values to database tankFixoperation

                            // _opList_history.Add(row);
                            db.AddToTankFixOperations_history(row);
                            //      MessageBox.Show("Title" + item.SubItems[0].ToString());

                        }

                        db.TankFixOperations.Where(x => x.tankID == tanktofix.Id).ToList().ForEach(db.TankFixOperations.DeleteObject);
                        if (db.SaveChanges() > 0)
                        {
                            MessageBox.Show("פעולה הצליחה");
                        }


                    }
                    else
                    {
                        if (db.SaveChanges() > 0)
                        {
                            MessageBox.Show("פעולה הצליחה");
                        }
                    }

                }
                else {
                    MessageBox.Show("צובר לא קיים");
                    return;
                }
            }
        }
    }
}